package view.library
{
	import events.library.EventResolveLibraryConflict;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.system.LoaderContext;
	
	import model.ModelLibrary;
	import model.ModelLibraryConflicts;
	
	import mx.core.FlexGlobals;
	import mx.events.CollectionEvent;
	import mx.managers.PopUpManager;
	
	import org.flashIso.engine.library.objects.LBase;
	import org.flashIso.engine.library.objects.LBinaryData;
	import org.flashIso.engine.library.objects.LImage;
	import org.flashIso.engine.library.objects.LObjectBase;
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Group;
	

	public class MediatorPopupLibraryConflicts extends Mediator
	{

		[Inject]
		public var mlc:ModelLibraryConflicts;
		
		[Inject]
		public var ml:ModelLibrary;
		
		[Inject]
		public var v:PopupLibraryConflicts;
		
		private var item:LBinaryData;
		
		override public function onRegister() : void  {
			if (mlc.conflicImages.length + mlc.conflictBinaries.length <= 1){
				(v.btnCancelAll.parent as Group).removeElement(v.btnCancelAll);
				(v.btnRenameAll.parent as Group).removeElement(v.btnRenameAll);
				(v.btnUpdateAll.parent as Group).removeElement(v.btnUpdateAll);
			}
			eventMap.mapListener(v.btnCancel, MouseEvent.CLICK, onClick);
			eventMap.mapListener(v.btnCancelAll, MouseEvent.CLICK, onClick);
			eventMap.mapListener(v.btnRename, MouseEvent.CLICK, onClick);
			eventMap.mapListener(v.btnRenameAll, MouseEvent.CLICK, onClick);
			eventMap.mapListener(v.btnUpdate, MouseEvent.CLICK, onClick);
			eventMap.mapListener(v.btnUpdateAll, MouseEvent.CLICK, onClick);
			
			if (mlc.conflicImages.length > 0){
				item = mlc.conflicImages.getItemAt(0) as LImage;
				v.currentState = "image";
				var context:LoaderContext = new LoaderContext();
				context.allowCodeImport = true;
				v.loaderNew.loaderContext = context;
				v.loaderCurrent.loaderContext = context;
				
				v.loaderNew.source = (item as LImage).data;
				v.loaderCurrent.source = ml.getLImageByID((item as LImage).id).data;
			}else{
				item = mlc.conflictBinaries.getItemAt(0) as LBinaryData;
				v.currentState = "binary"
				v.textAreaNew.text = (item as LBinaryData).data.toString().substr(1, 10000);
				v.textAreaCurrent.text = ml.getLBinaryDataByID((item as LBinaryData).id).data.toString().substr(1, 10000);
			}
			v.validateNow();
		}
		private function onClick(e:MouseEvent) : void {
			switch(e.target)
			{
				case v.btnCancel:
				{
					dispatch(new EventResolveLibraryConflict(EventResolveLibraryConflict.RESOLVE_LIBRARY_CONFLICT, item, EventResolveLibraryConflict.RESOLUTION_CANCEL));
					break;
				}
				case v.btnCancelAll:
				{
					dispatch(new EventResolveLibraryConflict(EventResolveLibraryConflict.RESOLVE_LIBRARY_CONFLICT, item, EventResolveLibraryConflict.RESOLUTION_CANCEL_ALL));
					break;
				}
				case v.btnRename:
				{
					dispatch(new EventResolveLibraryConflict(EventResolveLibraryConflict.RESOLVE_LIBRARY_CONFLICT, item, EventResolveLibraryConflict.RESOLUTION_RENAME));
					break;
				}
				case v.btnRenameAll:
				{
					dispatch(new EventResolveLibraryConflict(EventResolveLibraryConflict.RESOLVE_LIBRARY_CONFLICT, item, EventResolveLibraryConflict.RESOLUTION_RENAME_ALL));
					break;
				}
				case v.btnUpdate:
				{
					dispatch(new EventResolveLibraryConflict(EventResolveLibraryConflict.RESOLVE_LIBRARY_CONFLICT, item, EventResolveLibraryConflict.RESOLUTION_UPDATE));
					break;
				}
				case v.btnUpdateAll:
				{
					dispatch(new EventResolveLibraryConflict(EventResolveLibraryConflict.RESOLVE_LIBRARY_CONFLICT, item, EventResolveLibraryConflict.RESOLUTION_UPDATE_ALL));
					break;
				}
			}
			v.dispatchEvent(new Event(Event.CLOSE));
		}
	}
}